name: Build Probackup

on:
  push:
    branches:
      - "**"
  # Runs triggered by pull requests are disabled to prevent executing potentially unsafe code from public pull requests
  # pull_request:
  #   branches:
  #     - main

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:

  build-win2019:

    runs-on:
      - windows-2019

    env:
      zlib_dir: C:\dep\zlib

    steps:

      - uses: actions/checkout@v2

      - name: Install pacman packages
        run: |
          $env:PATH += ";C:\msys64\usr\bin"
          pacman -S --noconfirm --needed bison flex

      - name: Make zlib
        run: |
          git clone -b v1.2.11 --depth 1 https://github.com/madler/zlib.git
          cd zlib
          cmake -DCMAKE_INSTALL_PREFIX:PATH=C:\dep\zlib -G "Visual Studio 16 2019" .
          cmake --build . --config Release --target ALL_BUILD
          cmake --build . --config Release --target INSTALL
          copy C:\dep\zlib\lib\zlibstatic.lib C:\dep\zlib\lib\zdll.lib
          copy C:\dep\zlib\bin\zlib.dll C:\dep\zlib\lib

      - name: Get Postgres sources
        run: git clone -b REL_14_STABLE https://github.com/postgres/postgres.git

      # Copy ptrack to contrib to build the ptrack extension
      # Convert line breaks in the patch file to LF otherwise the patch doesn't apply
      - name: Get Ptrack sources
        run: |
          git clone -b master --depth 1 https://github.com/postgrespro/ptrack.git
          Copy-Item -Path ptrack -Destination postgres\contrib -Recurse
          (Get-Content ptrack\patches\REL_14_STABLE-ptrack-core.diff -Raw).Replace("`r`n","`n") | Set-Content ptrack\patches\REL_14_STABLE-ptrack-core.diff -Force -NoNewline
          cd postgres
          git apply -3 ../ptrack/patches/REL_14_STABLE-ptrack-core.diff

      - name: Build Postgres
        run: |
          $env:PATH += ";C:\msys64\usr\bin"
          cd postgres\src\tools\msvc
          (Get-Content config_default.pl) -Replace "zlib *=>(.*?)(?=,? *#)", "zlib => '${{ env.zlib_dir }}'" | Set-Content config.pl
          cmd.exe /s /c "`"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat`" amd64 && .\build.bat"

      - name: Build Probackup
        run: cmd.exe /s /c "`"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat`" amd64 && perl .\gen_probackup_project.pl `"${{ github.workspace }}`"\postgres"

      - name: Install Postgres
        run: |
          cd postgres
          src\tools\msvc\install.bat postgres_install

      - name: Install Testgres
        run: |
          git clone -b no-port-for --single-branch --depth 1 https://github.com/postgrespro/testgres.git
          pip3 install psycopg2 ./testgres

      # Grant the Github runner user full control of the workspace for initdb to successfully process the data folder
      - name: Test Probackup
        run: |
          icacls.exe "${{ github.workspace }}" /grant "${env:USERNAME}:(OI)(CI)F"
          $env:PATH += ";${{ github.workspace }}\postgres\postgres_install\lib;${{ env.zlib_dir }}\lib"
          $Env:LC_MESSAGES = "English"
          $Env:PG_CONFIG = "${{ github.workspace }}\postgres\postgres_install\bin\pg_config.exe"
          $Env:PGPROBACKUPBIN = "${{ github.workspace }}\postgres\Release\pg_probackup\pg_probackup.exe"
          $Env:PG_PROBACKUP_PTRACK = "ON"
          If (!$Env:MODE -Or $Env:MODE -Eq "basic") {
            $Env:PG_PROBACKUP_TEST_BASIC = "ON"
            python -m unittest -v tests
            python -m unittest -v tests.init_test
          } else {
            python -m unittest -v tests.$Env:MODE
          }

